package com.eqdd.yiqidian.ui.gongneng.renshi.train;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.util.SparseArray;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.TrainSignInfoActivityCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.SignListBean;
import com.eqdd.yiqidian.bean.rebuild.TrainSignUserListBean;
import com.eqdd.yiqidian.bean.rebuild.TrainSignUserListBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.BottomSheetUtil;
import com.eqdd.yiqidian.utils.ClickUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 18-2-3  下午2:38.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class TrainSignInfoActivity extends BaseActivity {

    private TrainSignInfoActivityCustom dataBinding;
    private BottomSheetDialog bottomSheet;
    private int pageNum;
    private int id;
    private int urlType = -1;
    private int type;
    private MVCCoolHelper<List<TrainSignUserListBean>> mvcHelper;
    private SignListBean signListBean;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_train_sign_info);
        initTopTitleBar(View.VISIBLE, "培训考勤统计与分析");
    }

    @Override
    public void initData() {
        id = getIntent().getIntExtra(Config.ID, 0);
        String theme = getIntent().getStringExtra(Config.THEME);
        signListBean = (SignListBean) getIntent().getSerializableExtra(Config.BEAN);

        dataBinding.tvTheme.setText(theme);
        SparseArray<String> urls = new SparseArray<>();
        urls.put(-1, "Training/Get_signInDetails.ashx");
        urls.put(0, "Training/Get_signDetail_sOrns.ashx");
        urls.put(1, "Training/Get_signDetail_sOrns.ashx");
        urls.put(2, "Training/Get_signDetail_OBT.ashx");
        urls.put(3, "Training/Get_signDetail_OBT.ashx");
        SparseArray<String> status = new SparseArray<>();
        status.put(1, "正常");
        status.put(-2, "未到");
        status.put(-1, "迟到");

        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new DividerLineItemDecoration(this));
        SlimAdapterEx<TrainSignUserListBean> slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_111, new SlimInjector<TrainSignUserListBean>() {
            @Override
            public void onInject(TrainSignUserListBean data, IViewInjector injector) {
                injector.text(R.id.tv_upper_left_content, data.getUserName())
                        .text(R.id.tv_down_left_content, data.getDepName() + "-" + data.getPostName())
                        .text(R.id.tv_upper_right_content, status.get(data.getStatus()))
                        .textColor(R.id.tv_upper_right_content, data.getStatus() == 1 ? Color.BLACK : Color.RED)
                        .visibility(R.id.bt_upper_center, data.getStatus() == -2 ? View.VISIBLE : View.GONE)
                        .text(R.id.tv_down_right_content, data.getSignInTime())
                        .visibility(R.id.tv_down_right_content, data.getStatus() == -2 ? View.INVISIBLE : View.VISIBLE);
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<TrainSignUserListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<ZhangPageResultBean<List<TrainSignUserListBean>>>post(HttpConfig.BASE_URL + urls.get(urlType))
                        .params("page", pageNum)
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("siInfoId", id)
                        .params("type", type)
                        .converter(new JsonConverter<ZhangPageResultBean<List<TrainSignUserListBean>>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<>())
                        .flatMap(listResultBean -> {
                            pageNum = listResultBean.getNextpage();
                            if (listResultBean.getStatus() == 200) {
                                return Flowable.just(listResultBean.getItems() == null ? new ArrayList<TrainSignUserListBean>() : listResultBean.getItems());
                            } else {
                                ToastUtil.showShort(listResultBean.getMsg());
                                return Flowable.just(new ArrayList<>());
                            }
                        });

            }
        });
        slimAdapterEx.updateData(new ArrayList<>());
        mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();

    }

    @Override
    public void setView() {

        ClickUtil.click(dataBinding.btFilter, () -> {
            if (bottomSheet == null) {
                bottomSheet = BottomSheetUtil.getBottomSheet(this, "查看方式", (recyclerView, position, v) -> {
                    bottomSheet.hide();
                    if (position != urlType) {
                        urlType = position;
                        switch (position) {
                            case 0:
                                type = 1;
                                break;
                            case 1:
                                type = 0;
                                break;
                            case 2:
                                type = 0;
                                break;
                            case 3:
                                type = 1;
                                break;
                        }
                        mvcHelper.refresh();
                    }
                }, "未签到", "已签到", "按时间顺序", "按时间倒序");
            }
            bottomSheet.show();
        });
        ClickUtil.click(dataBinding.ivErweima, () -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable(Config.BEAN, signListBean);
            startActivityForResult(QRSignActivity.class, bundle, Config.QR_SIGN);
        });
    }
}
